"""
OutputFixingParser输出解析器包装了另一个输出解析器，如果第一个解析器失败，它会调用另一个大型语言模型来修复任何错误。
但我们可以做其他事情，而不仅仅是抛出错误。具体来说，我们可以将格式错误的输出和格式化的指令一起传递给模型，并要求它进行修复。
"""
from typing import List

from langchain.output_parsers import OutputFixingParser
from langchain_core.output_parsers import PydanticOutputParser
from pydantic import BaseModel, Field

from models import get_ds_model_client


class Actor(BaseModel):
    name: str = Field(description="name of an actor")
    film_names: List[str] = Field(description="list of names of films they starred in")

actor_query = "Generate the filmography for a random actor."

parser = PydanticOutputParser(pydantic_object=Actor)
# 正确的格式应该是'{"name": "Tom Hanks", "film_names": ["Forrest Gump"]}'
misformatted = "{'name': 'Tom Hanks', 'film_names': ['Forrest Gump']}"
# print(parser.3-parse(misformatted))

client = get_ds_model_client()
# 传递给大模型解决问题
new_parser = OutputFixingParser.from_llm(parser=parser, llm=client)
print(new_parser.parse(misformatted))
